package jzqb.service.impl;


import haha.life.common.BelongToSqlUtils;
import haha.life.common.CutPageBean;
import jzqb.bo.StandardBo;
import jzqb.bo.SubscriptBo;
import jzqb.bo.SubscriptDataBo;
import jzqb.dao.SubscriptDao;
import jzqb.dao.SubscriptDataDao;
import jzqb.service.SubscriptDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 自用库业务层实现类
 */
@Service
public class SubscriptDataServiceImpl implements SubscriptDataService {
    @Autowired
    SubscriptDataDao subscriptDataDao;

    @Value("${Limit.number}")
    private Integer limitNumber;

    @Value("${Limit.time}")
    private Integer limitTime;

    @Autowired
    SubscriptDao subscriptDao;

    @Override
    public Boolean insertSubscriptData(SubscriptDataBo subscriptDataBo) {
        String now = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        subscriptDataBo.setReadTime(now);
        return subscriptDataDao.insertSubscriptData(subscriptDataBo);
    }

    @Override
    public SubscriptDataBo getSubscriptDataByCondition(SubscriptDataBo subscriptDataBo) {
        return subscriptDataDao.getSubscriptDataByCondition(subscriptDataBo);
    }

    @Override
    public CutPageBean<SubscriptDataBo> getReadSubscriptData(SubscriptDataBo subscriptDataBo) {
        CutPageBean<SubscriptDataBo> cutPageBean = new CutPageBean<>();
        subscriptDataBo.setStart((subscriptDataBo.getPage() - 1) * subscriptDataBo.getPageSize());
        List<SubscriptDataBo> subscriptDataBoss = subscriptDataDao.getReadSubscriptData(subscriptDataBo);
        Long count = subscriptDataDao.getReadSubscriptDataCount(subscriptDataBo);
        cutPageBean.initCutPage(count.intValue(), subscriptDataBo.getPageSize(), subscriptDataBoss);
        return cutPageBean;
    }

    @Override
    public CutPageBean<StandardBo> getNoReadSubscriptData(SubscriptDataBo subscriptDataBo) {
        CutPageBean<StandardBo> cutPageBean = new CutPageBean<>();
        //这里去设置前500条的数据。
        subscriptDataBo.setLimitDataNumber(limitNumber);
        //这里需要去获得此领域的订阅时间
        SubscriptBo subscriptBo = subscriptDao.getSubScriptBoByCondition(subscriptDataBo);
        if (subscriptBo == null) {
            cutPageBean.initCutPage(0, subscriptDataBo.getPageSize(), new ArrayList<>());
            return cutPageBean;
        }
        String time = subscriptBo.getSubscriptTime();
        subscriptDataBo.setSubscriptTime(time);
        //根据资源库 还有领域 时间倒序 查询出总量500 条信息
        List<StandardBo> readList = subscriptDataDao.getNewData(subscriptDataBo);
        if (readList.size() > 0) {
            StringBuffer bufferRes = new StringBuffer();
            String result = "";
            bufferRes.append(" AND ( ");
            for (StandardBo standardBo : readList) {
                bufferRes.append(" 资源ID= ").append(standardBo.getSysID()).append(" or");
            }
            if (bufferRes.toString().endsWith("or")) {
                result = bufferRes.substring(0, bufferRes.length() - 2);
            } else {
                result = bufferRes.toString();
            }
            result = result + " )";
            subscriptDataBo.setFinalDataStr(result);
        }
        // 然后通过500 条拼接 获得 这500 条中 已读的ID
        List<String> readIdList = subscriptDataDao.getReadIdList(subscriptDataBo);
        List<String> notConclude = new ArrayList<>();
        // 然后再通过已读ID 去寻找未读ID
        for (StandardBo standardBo : readList) {
            if (!readIdList.contains(standardBo.getSysID().toString())) {
                notConclude.add(standardBo.getSysID().toString());
            }
        }
        subscriptDataBo.setStart((subscriptDataBo.getPage() - 1) * subscriptDataBo.getPageSize());
        //这里拼接Equals 的语句
        String string = BelongToSqlUtils.getBelongToSql(notConclude.toArray(new String[notConclude.size()]), "SYS_FLD_SYSID");
//        String notString = BelongToSqlUtils.getNotBelongToSql(readIdList.toArray(new String[readIdList.size()]), "SYS_FLD_SYSID");
        if (string == null || string.equals("")) {
             cutPageBean.initCutPage(0, subscriptDataBo.getPageSize(), new ArrayList<StandardBo>());
            return cutPageBean;
        }
        string =  "(" + string + ")";
        subscriptDataBo.setFinalDataStr(string);
        //这里进行分页效果
        
        List<StandardBo> finalResult = subscriptDataDao.getFinalResult(subscriptDataBo);
        Long count = subscriptDataDao.getFinalResultCount(subscriptDataBo);
        cutPageBean.initCutPage(count.intValue(), subscriptDataBo.getPageSize(), finalResult);
        return cutPageBean;
    }
}